﻿using Common;
using DataAccess;
using Entity;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UI;

namespace DataBusiness
{
    public class WellStructBLL
    {
        /// <summary>
        /// 得到井深结构表
        /// </summary>
        /// <returns></returns>
        public static DataSet GetWellStruct()
        {
            string sql = "select * from tb_HoleStructure";
            DataSet initDt;
            initDt = SqlHelper.QuerySql(sql);
            initDt.Tables[0].Columns[0].ColumnName = "ID";
            initDt.Tables[0].Columns[1].ColumnName = "井号";
            initDt.Tables[0].Columns[2].ColumnName = "套管程序";
            initDt.Tables[0].Columns[3].ColumnName = "井眼尺寸";
            initDt.Tables[0].Columns[4].ColumnName = "完钻深度";
            initDt.Tables[0].Columns[5].ColumnName = "套管尺寸";
            initDt.Tables[0].Columns[6].ColumnName = "开始位置";
            initDt.Tables[0].Columns[7].ColumnName = "结束位置";
            initDt.Tables[0].Columns[8].ColumnName = "套管鞋层位";
            initDt.Tables[0].Columns[9].ColumnName = "水泥返深";
            initDt.Tables[0].Columns[10].ColumnName = "添加人";
            initDt.Tables[0].Columns[11].ColumnName = "添加时间";
            initDt.Tables[0].Columns[12].ColumnName = "压裂压力钻井液密度";
            initDt.Tables[0].Columns[13].ColumnName = "下次钻井目的井深";
            initDt.Tables[0].Columns[14].ColumnName = "下次钻井所用最高钻井液密度";
            return initDt;
        }

        public static DataTable GetWellStructByWellNo(string wellNo)
        {
            string sSql = "select * from tb_HoleStructure where WellNo = @WellNo";
            SqlParameter[] Paras = new SqlParameter[1];
            Paras[0] = new SqlParameter("@WellNo", wellNo);
            DataTable ds = SqlHelper.QuerySql(sSql, Paras).Tables[0];
            ds.Columns[0].ColumnName = "ID";
            ds.Columns[1].ColumnName = "井号";
            ds.Columns[2].ColumnName = "套管程序";
            ds.Columns[3].ColumnName = "井眼尺寸";
            ds.Columns[4].ColumnName = "完钻深度";
            ds.Columns[5].ColumnName = "套管尺寸";
            ds.Columns[6].ColumnName = "开始位置";
            ds.Columns[7].ColumnName = "结束位置";
            ds.Columns[8].ColumnName = "套管鞋层位";
            ds.Columns[9].ColumnName = "水泥返深";
            ds.Columns[10].ColumnName = "添加人";
            ds.Columns[11].ColumnName = "添加时间";
            ds.Columns[12].ColumnName = "压裂压力钻井液密度";
            ds.Columns[13].ColumnName = "下次钻进目的井深";
            ds.Columns[14].ColumnName = "下次钻井所用最高钻井液密度";

            DataTable retDt = new DataTable();
            retDt.Columns.Add(new DataColumn("井号", typeof(string)));
            retDt.Columns.Add(new DataColumn("套管程序", typeof(string)));
            retDt.Columns.Add(new DataColumn("ID", typeof(string)));
            retDt.Columns.Add(new DataColumn("套管尺寸", typeof(string)));
            retDt.Columns.Add(new DataColumn("下入深度", typeof(string)));
            retDt.Columns.Add(new DataColumn("下次钻井所用最高钻井液密度", typeof(string)));
            retDt.Columns.Add(new DataColumn("下次钻进目的井深", typeof(string)));

            DataRow dr;
            foreach (DataRow row in ds.Rows)
            {
                dr = retDt.NewRow();
                dr["井号"] = row["井号"];
                dr["套管程序"] = row["套管程序"];
                dr["ID"] = row["ID"];
                dr["套管尺寸"] = row["套管尺寸"];
                dr["下入深度"] = row["结束位置"];
                dr["下次钻井所用最高钻井液密度"] = row["下次钻井所用最高钻井液密度"];
                dr["下次钻进目的井深"] = row["下次钻进目的井深"];
                retDt.Rows.Add(dr);
            }
            return retDt;

        }

        /// <summary>
        /// 修改井深结构
        /// </summary>
        /// <param name="wellStructModel"></param>
        /// <returns>返回所影响的数据行数</returns>
        public static int UpdateWellStruct(tb_WellStructModel wellStructModel)
        {
            string sSql = @"update tb_HoleStructure set
                            WellNo = @WellNo,
                            CasingProgram = @CasingProgram,
                            HoleSize = @HoleSize,
                            TotalDepth = @TotalDepth,
                            WellSize = @WellSize,
                            StartLocation = @StartLocation,
                            EndLocation = @EndLocation,
                            CasingShoe = @CasingShoe,
                            CementDepth = @CementDepth,
                            UserID = @UserID,
                            InputDate = @InputDate,
                            FracturePressureDrillingFluidDensity = @FracturePressureDrillingFluidDensity,
                            WellDepthOfTheNextDrilling = @WellDepthOfTheNextDrilling,
                            MaximumDrillingFluidDensityForNextDrilling = @MaximumDrillingFluidDensityForNextDrilling
                            where ID = @ID";
            SqlParameter[] Paras = new SqlParameter[15];
            Paras[0] = new SqlParameter("@WellNo", wellStructModel.WellNo);
            Paras[1] = new SqlParameter("@CasingProgram", wellStructModel.CasingProgram);
            Paras[2] = new SqlParameter("@HoleSize", wellStructModel.HoleSize);
            Paras[3] = new SqlParameter("@TotalDepth", wellStructModel.TotalDepth);
            Paras[4] = new SqlParameter("@WellSize", wellStructModel.WellSize);
            Paras[5] = new SqlParameter("@StartLocation", wellStructModel.StartLocation);
            Paras[6] = new SqlParameter("@EndLocation", wellStructModel.EndLocation);
            Paras[7] = new SqlParameter("@CasingShoe", wellStructModel.CasingShoe);
            Paras[8] = new SqlParameter("@CementDepth", wellStructModel.CementDepth);
            Paras[9] = new SqlParameter("@UserID", wellStructModel.UserID);
            Paras[10] = new SqlParameter("@InputDate", wellStructModel.InputDate);
            Paras[11] = new SqlParameter("@FracturePressureDrillingFluidDensity", wellStructModel.Fpdfd);
            Paras[12] = new SqlParameter("@WellDepthOfTheNextDrilling", wellStructModel.Wdotnd);
            Paras[13] = new SqlParameter("@MaximumDrillingFluidDensityForNextDrilling", wellStructModel.Mdfdfnd);
            Paras[14] = new SqlParameter("@ID", wellStructModel.ID);
            return SqlHelper.ExeSql(sSql, Paras);
        }

        /// <summary>
        /// 添加井深结构
        /// </summary>
        /// <param name=""></param>
        /// <returns>返回所影响的数据行数</returns>
        public static int InsertWellStruct(tb_WellStructModel wellStructModel)
        {
            string sSql = "Insert into tb_HoleStructure(WellNo,CasingProgram,HoleSize,TotalDepth,WellSize,StartLocation,EndLocation,CasingShoe,CementDepth,UserID,InputDate,FracturePressureDrillingFluidDensity,WellDepthOfTheNextDrilling,MaximumDrillingFluidDensityForNextDrilling) values(@WellNo,@CasingProgram,@HoleSize,@TotalDepth,@WellSize,@StartLocation,@EndLocation,@CasingShoe,@CementDepth,@UserID,@InputDate,@FracturePressureDrillingFluidDensity,@WellDepthOfTheNextDrilling,@MaximumDrillingFluidDensityForNextDrilling)";
            SqlParameter[] Paras = new SqlParameter[14];
            //Paras[0] = new SqlParameter("@WellNo", wellStructModel.ID);
            Paras[0] = new SqlParameter("@WellNo", wellStructModel.WellNo);
            Paras[1] = new SqlParameter("@CasingProgram", wellStructModel.CasingProgram);
            Paras[2] = new SqlParameter("@HoleSize", wellStructModel.HoleSize);
            Paras[3] = new SqlParameter("@TotalDepth", wellStructModel.TotalDepth);
            Paras[4] = new SqlParameter("@WellSize", wellStructModel.WellSize);
            Paras[5] = new SqlParameter("@StartLocation", wellStructModel.StartLocation);
            Paras[6] = new SqlParameter("@EndLocation", wellStructModel.EndLocation);
            Paras[7] = new SqlParameter("@CasingShoe", wellStructModel.CasingShoe);
            Paras[8] = new SqlParameter("@CementDepth", wellStructModel.CementDepth);
            Paras[9] = new SqlParameter("@UserID", wellStructModel.UserID);
            Paras[10] = new SqlParameter("@InputDate", wellStructModel.InputDate);
            Paras[11] = new SqlParameter("@FracturePressureDrillingFluidDensity", wellStructModel.Fpdfd);
            Paras[12] = new SqlParameter("@WellDepthOfTheNextDrilling", wellStructModel.Wdotnd);
            Paras[13] = new SqlParameter("@MaximumDrillingFluidDensityForNextDrilling", wellStructModel.Mdfdfnd);
            return SqlHelper.ExeSql(sSql, Paras);
        }
        /// <summary>
        /// 删除选中记录
        /// </summary>
        /// <param name="wellStructId"></param>
        /// <returns>返回所影响的数据行数</returns>
        public static int DeleteWellStruct(string ID)
        {
            string sSql = string.Format(("delete from tb_HoleStructure where ID= '{0}'"), ID);
            return SqlHelper.ExeSql(sSql);
        }

        public static  DataSet GetWellStructByCasingProgram(string CasingProgram)
        {
            string sql = "select * from tb_HoleStructure where CasingProgram = '"+ CasingProgram + "'";
            DataSet initDt;
            initDt = SqlHelper.QuerySql(sql);
            initDt.Tables[0].Columns[0].ColumnName = "ID";
            initDt.Tables[0].Columns[1].ColumnName = "井号";
            initDt.Tables[0].Columns[2].ColumnName = "套管程序";
            initDt.Tables[0].Columns[3].ColumnName = "井眼尺寸";
            initDt.Tables[0].Columns[4].ColumnName = "完钻深度";
            initDt.Tables[0].Columns[5].ColumnName = "套管尺寸";
            initDt.Tables[0].Columns[6].ColumnName = "开始位置";
            initDt.Tables[0].Columns[7].ColumnName = "结束位置";
            initDt.Tables[0].Columns[8].ColumnName = "套管鞋层位";
            initDt.Tables[0].Columns[9].ColumnName = "水泥返深";
            initDt.Tables[0].Columns[10].ColumnName = "添加人";
            initDt.Tables[0].Columns[11].ColumnName = "添加时间";
            initDt.Tables[0].Columns[12].ColumnName = "压裂压力钻井液密度";
            initDt.Tables[0].Columns[13].ColumnName = "下次钻井目的井深";
            initDt.Tables[0].Columns[14].ColumnName = "下次钻井所用最高钻井液密度";
            return initDt;
        }
    }
}
